//
//  ViewController.m
//  19-Homework
//
//  Created by dllo on 16/8/9.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
#import "BrandList.h"
#import "List.h"
#import "CarsTableViewCell.h"
#import "HotBrandTableViewCell.h"
#define kTableViewReuse @"reuse"
#define kHotBrandTableViewCell @"brandReuse"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *carTableView;
@property (nonatomic, strong) NSMutableArray<BrandList *> *scetionArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scetionArray = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = NO;
    [self getAllCarsDataFromJSON];
    [self creatTableView];
}

- (void)getAllCarsDataFromJSON {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"All" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSError *error = nil;
    NSDictionary *dicJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSDictionary *resultDic = [dicJSON objectForKey:@"result"];
    NSArray *brandListArray = [resultDic objectForKey:@"brandlist"];
    
    for (NSDictionary *dictionary in brandListArray) {
        BrandList *brandList = [BrandList brandListWithDictionary:dictionary];
        [_scetionArray addObject:brandList];
        
        NSMutableArray *listArr = [NSMutableArray array];
        NSArray *listArray = [dictionary objectForKey:@"list"];
        for (NSDictionary *dictionary in listArray) {
            List *list = [List listWithDictionary:dictionary];
            [listArr addObject:list];
            brandList.list = [NSArray arrayWithArray:listArr];
        }
    }
}

- (void)creatTableView {
    self.carTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _carTableView.delegate = self;
    _carTableView.dataSource = self;
    [self.view addSubview:_carTableView];
    [_carTableView registerClass:[HotBrandTableViewCell class] forCellReuseIdentifier:kHotBrandTableViewCell];
    [_carTableView registerNib:[UINib nibWithNibName:@"CarsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kTableViewReuse];
}

#pragma mark - TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _scetionArray.count + 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"热门品牌";
    } else {
        return [_scetionArray[section - 1] letter];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    } else {
        return [_scetionArray[section - 1] list].count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        HotBrandTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHotBrandTableViewCell];
        return cell;
    } else {
    CarsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    List *list = [_scetionArray[indexPath.section - 1] list][indexPath.row];
    cell.list = list;
    return cell;
    }
}

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSMutableArray *indexArray = [NSMutableArray array];
    for (BrandList *brandList in _scetionArray) {
        [indexArray addObject:brandList.letter];
    }
    return indexArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
